// pages/modifysingleinfo/modifysingleinfo.js
const db = wx.cloud.database()
const user = db.collection('user')
const club = db.collection('club')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemvalue:'',
    itemname:'',
    temp:'',
    errormessage:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var modifyitem = options.modifyitem
    var itemcname = options.itemcname
    console.log(modifyitem)
    var _this = this
    if (itemcname == 'openid'){
      wx.cloud.callFunction({
        name: 'readclub',
        data: {
          _id: modifyitem
        },
        success(res) {
          console.log(res.result)
          if (res.result.data.administrator1 != null || res.result.data.administrator1 != ""){
            _this.setData({
              itemvalue: res.result.data.administrator1,
              itemname: itemcname,
              temp: res.result.data.administrator1,
              clubid: modifyitem
            })
          }else{
            _this.setData({
              itemvalue: '',
              itemname: itemcname,
              temp: '',
              clubid: modifyitem
            })
          }
        },
        fail: console.error
      })
    } else if (itemcname == '管理员2openid'){
      wx.cloud.callFunction({
        name: 'readclub',
        data: {
          _id: modifyitem
        },
        success(res) {
          if (res.result.data.administrator2 != null || res.result.data.administrator2 != "") {
            _this.setData({
              itemvalue: res.result.data.administrator2,
              itemname: itemcname,
              temp: res.result.data.administrator2,
              clubid: modifyitem
            })
          } else {
            _this.setData({
              itemvalue: '',
              itemname: itemcname,
              temp: '',
              clubid: modifyitem
            })
          }
        },
        fail: console.error
      })
    } else if (itemcname == '社长openid'){
      wx.cloud.callFunction({
        name: 'readclub',
        data: {
          _id: modifyitem
        },
        success(res) {
          console.log(res.result)
          if (res.result.data.master != null || res.result.data.master != "") {
            _this.setData({
              itemvalue: res.result.data.master,
              itemname: itemcname,
              temp: res.result.data.master,
              clubid: modifyitem
            })
          } else {
            _this.setData({
              itemvalue: '',
              itemname: itemcname,
              temp: '',
              clubid: modifyitem
            })
          }
        },
        fail: console.error
      })
    }
    else{
      user.get().then(res => {
        if (itemcname == '姓名') {
          _this.setData({
            itemname: itemcname,
            itemvalue: res.data[0].extraUserInfo.username,
            temp: res.data[0].extraUserInfo.username
          })
        } else if (itemcname == '学号') {
          _this.setData({
            itemname: itemcname,
            itemvalue: res.data[0].extraUserInfo.userstuid,
            temp: res.data[0].extraUserInfo.userstuid
          })
        } else if (itemcname == '手机') {
          _this.setData({
            itemname: itemcname,
            itemvalue: res.data[0].extraUserInfo.userphone,
            temp: res.data[0].extraUserInfo.userphone
          })
        }
      })
    }
  },

  onChange: function (e) {
    var _this = this
    _this.setData({
      temp: e.detail
    }, () => {
      console.log(_this.data.temp)
    })
  },

  onSubmit: function (e) {
    var _this = this
    switch (_this.data.itemname){
      case '姓名':
        if (_this.data.temp == null || _this.data.temp == "") {
          _this.setData({
            errormessage: '格式错误'
          })
        } else {
          var _id = ''
          user.get().then(res => {
            _id = res.data[0]._id
            user.doc(_id).update({
              data: {
                'extraUserInfo.username': _this.data.temp
              },
              success: res => {
                var pages = getCurrentPages()
                if (pages.length > 1) {
                  var prePage = pages[pages.length - 2];
                  prePage.onLoad()
                }
                wx.navigateBack({ delta: 1 })
              },
              fail: console.error
            })
          })
        }
        break

      case '学号':
        if (_this.data.temp == null || _this.data.temp == "") {
          _this.setData({
            errormessage: '格式错误'
          })
        } else {
          var _id = ''
          user.get().then(res => {
            _id = res.data[0]._id
            user.doc(_id).update({
              data: {
                'extraUserInfo.userstuid': _this.data.temp
              },
              success: res => {
                var pages = getCurrentPages()
                if (pages.length > 1) {
                  var prePage = pages[pages.length - 2];
                  prePage.onLoad()
                }
                wx.navigateBack({ delta: 1 })
              },
              fail: console.error
            })
          })
        }
        break

      case '手机':
        if (_this.data.temp == null || _this.data.temp == "") {
          _this.setData({
            errormessage: '格式错误'
          })
        } else {
          var _id = ''
          user.get().then(res => {
            _id = res.data[0]._id
            user.doc(_id).update({
              data: {
                'extraUserInfo.userphone': _this.data.temp
              },
              success: res => {
                var pages = getCurrentPages()
                if (pages.length > 1) {
                  var prePage = pages[pages.length - 2];
                  prePage.onLoad()
                }
                wx.navigateBack({ delta: 1 })
              },
              fail: console.error
            })
          })
        }
        break

      case 'openid':
        if (_this.data.temp == null || _this.data.temp == "") {
          _this.setData({
            errormessage: '格式错误'
          })
        } else if (_this.data.temp == _this.data.itemvalue) {
          var id = _this.data.clubid
          var pages = getCurrentPages()
          if (pages.length > 1) {
            var prePage = pages[pages.length - 2];
            prePage.setData({
              clubId: id,
            })
          }
          wx.navigateBack({ delta: 1 })
        } else {
          wx.cloud.callFunction({
            name: 'readuser',
            data: {
              _openid: _this.data.temp,
            },
            success(res) {
              console.log(res.result)
              if (res.result.data.length != 0) {
                if (res.result.data[0].clubManage != null) {
                  wx.showToast({
                    title: '该id已被绑定',
                    icon: 'none',
                    duration: 2000
                  })
                }else{
                    console.log('openid')
                    wx.cloud.callFunction({
                      name: 'updateclub',
                      data: {
                        _id: _this.data.clubid,
                        openid: _this.data.temp,
                        who: 1,
                      },
                      success(clubres) {
                        console.log(clubres)
                      },
                      fail: console.error
                    })
                    wx.cloud.callFunction({
                      name: 'updateuser',
                      data: {
                        _openid: _this.data.temp,
                        clubid: _this.data.clubid,
                      },
                      success(res) {
                        console.log("clubid", _this.data.clubid)
                        console.log(res.result)
                        if (res.result.stats.updated != 0) {
                          wx.showToast({
                            title: '设置成功',
                            icon: 'success',
                            duration: 2000
                          })
                        }
                      },
                      fail: console.error
                    })
                    var id = _this.data.clubid
                    var pages = getCurrentPages()
                    if (pages.length > 1) {
                      var prePage = pages[pages.length - 2];
                      prePage.onLoad({ id: _this.data.clubid })
                    }
                    wx.navigateBack({ delta: 1 })
                }

              } else {
                wx.showModal({
                  title: '提示',
                  content: '请输入正确openid',
                  showCancel: false
                })
              }
            },
            fail: console.error
          })
        }
        break

      case '管理员2openid':
        if (_this.data.temp == null || _this.data.temp == "") {
          _this.setData({
            errormessage: '格式错误'
          })
        } else if (_this.data.temp == _this.data.itemvalue) {
          var id = _this.data.clubid
          var pages = getCurrentPages()
          if (pages.length > 1) {
            var prePage = pages[pages.length - 2];
            prePage.setData({
              clubId: id,
            })
          }
          wx.navigateBack({ delta: 1 })
        } else {
          wx.cloud.callFunction({
            name: 'readuser',
            data: {
              _openid: _this.data.temp,
            },
            success(res) {
              console.log(res.result)
              if (res.result.data.length != 0) {
                if (res.result.data[0].clubManage != null) {
                  wx.showToast({
                    title: '该id已被绑定',
                    icon: 'none',
                    duration: 2000
                  })
                }else{
                console.log('openid')
                wx.cloud.callFunction({
                  name: 'updateclub',
                  data: {
                    _id: _this.data.clubid,
                    openid: _this.data.temp,
                    who: 2,
                  },
                  success(clubres) {
                    console.log(clubres)
                  },
                  fail: console.error
                })
                wx.cloud.callFunction({
                  name: 'updateuser',
                  data: {
                    _openid: _this.data.temp,
                    clubid: _this.data.clubid,
                  },
                  success(res) {
                    console.log("clubid", _this.data.clubid)
                    console.log(res.result)
                    if (res.result.stats.updated != 0) {
                      wx.showToast({
                        title: '设置成功',
                        icon: 'success',
                        duration: 2000
                      })
                    }
                  },
                  fail: console.error
                })
                var id = _this.data.clubid
                var pages = getCurrentPages()
                if (pages.length > 1) {
                  var prePage = pages[pages.length - 2];
                  prePage.onLoad({ id: _this.data.clubid })
                }
                wx.navigateBack({ delta: 1 })
                }

              } else {
                wx.showModal({
                  title: '提示',
                  content: '请输入正确openid',
                  showCancel: false
                })
              }
            },
            fail: console.error
          })
        }
        break

      case '社长openid':
        if (_this.data.temp == null || _this.data.temp == "") {
          _this.setData({
            errormessage: '格式错误'
          })
        }else if(_this.data.temp==_this.data.itemvalue){
          var id = _this.data.clubid
          var pages = getCurrentPages()
          if (pages.length > 1) {
            var prePage = pages[pages.length - 2];
            prePage.setData({
              clubId: id,
            })
          }
          wx.navigateBack({ delta: 1 })
        }else {
          wx.cloud.callFunction({
            name: 'readuser',
            data: {
              _openid: _this.data.temp,
            },
            success(res) {
              console.log(res.result)
              if(res.result.data.length!=0){
                if (res.result.data[0].clubManage!=null){
                  wx.showToast({
                    title: '该id已被绑定',
                    icon: 'none',
                    duration: 2000
                  })
                }
                else{
                console.log('openid')
                wx.cloud.callFunction({
                  name: 'updateclub',
                  data: {
                    _id: _this.data.clubid,
                    openid: _this.data.temp,
                    who: 3,
                  },
                  success(clubres) {
                    console.log(clubres)
                  },
                  fail: console.error
                })
                wx.cloud.callFunction({
                  name: 'updateuser',
                  data: {
                    _openid: _this.data.temp,
                    clubid: _this.data.clubid,
                  },
                  success(res) {
                    console.log("clubid", _this.data.clubid)
                    console.log(res.result)
                    if (res.result.stats.updated!=0){
                      wx.showToast({
                      title: '设置成功',
                      icon: 'success',
                      duration: 2000
                      })
                    }
                  },
                  fail: console.error
                })
                var id = _this.data.clubid
                var pages = getCurrentPages()
                if (pages.length > 1) {
                  var prePage = pages[pages.length - 2];
                  prePage.onLoad({ id: _this.data.clubid})
                }
                wx.navigateBack({ delta: 1 })
                }

              }else{
                wx.showModal({
                  title: '提示',
                  content: '请输入正确openid', 
                  showCancel:false
                })
              }
            },
            fail: console.error
          })
        }
        break
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})